package com.wzc.hc.controller;

import cn.dev33.satoken.stp.SaTokenInfo;
import cn.dev33.satoken.stp.StpUtil;
import com.wzc.hc.entity.req.LoginReq;
import com.wzc.hc.entity.resp.Result;
import com.wzc.hc.service.UserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpSession;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @author 13813
 */
@Tag(name = "用户登录", description = "用户登录相关")
@RestController
@RequestMapping("/login")
public class LoginController {
    @Resource
    UserService userService;
    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @Operation(summary = "用户登录")
    @PostMapping("/doLogin")
    public Result<String> doLogin(HttpSession session, @Validated @RequestBody LoginReq login) {
        String uuid = session.getAttribute("code:uuid").toString();
        String code = stringRedisTemplate.opsForValue().get("code:" + uuid);
        if (login.getCode().equals(code))
            return Result.ok(userService.login(login));
        return Result.fail("验证码错误！");
    }

    @GetMapping("/isLogin")
    public Result<String> isLogin() {
        return Result.ok("是否登录：" + StpUtil.isLogin());
    }

    @GetMapping("/tokenInfo")
    public Result<SaTokenInfo> tokenInfo() {
        return Result.ok(StpUtil.getTokenInfo());
    }

    @GetMapping("/logout")
    public Result<String> logout() {
        StpUtil.logout();
        return Result.ok("注销成功");
    }
}




